package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.HomeSearchActivity;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.bean.HomeClassBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.MyGridView;
import cn.xfdzx.android.apps.shop.view.MyRatingBar;

import static cn.xfdzx.android.apps.shop.apps.Constants.SHOPID_69_NO;

/**
 * @author gzk
 * @date 2020/4/6 0006
 * description：
 */
public class SearchShopsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private Context mContext;

    private int type;
    private List<HomeSearchXFDGoodBeen.DataBean> mList;

    public SearchShopsAdapter(Context mContext, List<HomeSearchXFDGoodBeen.DataBean> mList, int type) {
        this.mContext = mContext;
        this.mList = mList;
        this.type = type;
//        add();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder;
        if (type == 0) {
            view = View.inflate(parent.getContext(), R.layout.item_home_shop_list, null);
            holder = new Holder(view);
        } else {
            view = View.inflate(parent.getContext(), R.layout.item_home_xfd_shop_list, null);
            holder = new XfdHolder(view);
        }
        view.setOnClickListener(this);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        if (type == 0) {
            ((Holder) holder).tvHomeShopListTitle.setText(mList.get(position).getOffline_shop_name());
            Glide.with(mContext).load(mList.get(position).getLogo()).apply(options.transform(new CenterCrop(), roundedCorners)).into(((Holder) holder).ivHomeShopList);

            ((Holder) holder).tvHomeShopListLevel.setText(mList.get(position).getStar() + "");
            ((Holder) holder).tvHomeShopListMonthSales.setText("销量" + mList.get(position).getMonthly_sales());
            ((Holder) holder).tvHomeShopListUpSend.setText("起送¥" + mList.get(position).getSend_fees());
            ((Holder) holder).tvHomeShopListBasis.setText("基础运费¥" + mList.get(position).getBase_freight());
            ((Holder) holder).tvShunfeng.setText( mList.get(position).getExpress_name());
            ((Holder) holder).rbStartLevel.setRating(mList.get(position).getStar());
            if (mList.get(position).getIs_close()==0){
                ((Holder) holder).tvDayang.setVisibility(View.GONE);
            }else {
                ((Holder) holder).tvDayang.setVisibility(View.VISIBLE);
            }

            if (mList.get(position).getDistance()==null){
                ((Holder) holder).tvHomeShopListSendDistance.setVisibility(View.INVISIBLE);
                ((Holder) holder).tvHomeShopDistanceHuiLine.setVisibility(View.INVISIBLE);
            }else {
                ((Holder) holder).tvHomeShopDistanceHuiLine.setVisibility(View.VISIBLE);
                ((Holder) holder).tvHomeShopListSendDistance.setVisibility(View.VISIBLE);
                ((Holder) holder).tvHomeShopListSendDistance.setText(mList.get(position).getDistance() + "");
            }
//            ((Holder) holder).tvHomeShopListSendTime.setText(mList.get(position).send_time);
            //下面的getCoupon()字段没有给
//            ((Holder) holder).mgvHomeShopList.setAdapter(new HomeShopJuanGVAdapter(mContext, mList.get(position).getCoupon()));
            ((Holder) holder).mgvHomeGoodsList.setVisibility(View.VISIBLE);
            SearchShopGoodsGVAdapter searchShopGoodsGVAdapter = new SearchShopGoodsGVAdapter(mContext, mList.get(position).getGoods());
            ((Holder) holder).mgvHomeGoodsList.setAdapter(searchShopGoodsGVAdapter);
            ((Holder) holder).mgvHomeGoodsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                    Intent intent = new Intent(mContext, ShopActivity.class);
                    intent.putExtra("shop_id", mList.get(position).getId() + "");
                    intent.putExtra("good_id", mList.get(position).getGoods().get(i).getId() + "");
                    mContext.startActivity(intent);
                }
            });
            if (UtilsData.getPreference(mContext,"activity").getBoolean("is_69")&&!SHOPID_69_NO.contains(mList.get(position).getRole_id())){
                ((Holder) holder).tvHomeManJian.setVisibility(View.VISIBLE);
            }else {
                ((Holder) holder).tvHomeManJian.setVisibility(View.GONE);
            }
        } else {

//            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
            Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(((XfdHolder) holder).ivXfdGood);


            ((XfdHolder) holder).tvXfdName.setText(mList.get(position).getTitle());
//            ((XfdHolder) holder).tvXfdTag.setText(bean.getData().get(position).ma);
            if (Double.parseDouble(mList.get(position).getDiscountprice())==0){
                ((XfdHolder) holder).tvXfdGoodPrice.setText("¥" + mList.get(position).getMarketprice());
            }else {
                ((XfdHolder) holder).tvXfdGoodPrice.setText("¥" + mList.get(position).getDiscountprice());
            }
//            if (mList.get(position).getUnit().isEmpty()){
            ((XfdHolder) holder).tvXfdGoodsSaleNum.setText("已售" + mList.get(position).getSale_num() + "件");
//            }else {
//                ((XfdHolder) holder).tvXfdGoodsSaleNum.setText("已售"+mList.get(position).getSale_num()+mList.get(position).getUnit());
//            }
        }

        holder.itemView.setTag(position);
    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    public class Holder extends RecyclerView.ViewHolder {
        private TextView tvHomeManJian;
        private TextView tvHomeShopListTitle;
        private ImageView ivHomeShopList;
        private TextView tvHomeShopListLevel;
        private TextView tvHomeShopListMonthSales;
        private TextView tvHomeShopListUpSend;
        private TextView tvHomeShopListBasis;
        private TextView tvHomeShopListSendDistance;
        private TextView tvHomeShopDistanceHuiLine;
        private TextView tvHomeShopListSendTime;
        private TextView tvDayang;
        private TextView tvShunfeng;
        private MyGridView mgvHomeShopList;
        private MyGridView mgvHomeGoodsList;
        private MyRatingBar rbStartLevel;

        public Holder(View itemView) {
            super(itemView);
            tvHomeManJian = itemView.findViewById(R.id.tv_home_man_jian);
            rbStartLevel = itemView.findViewById(R.id.rb_start_level);
            tvHomeShopListTitle = itemView.findViewById(R.id.tv_home_shop_list_title);
            ivHomeShopList = itemView.findViewById(R.id.iv_home_shop_list);
            tvHomeShopListLevel = itemView.findViewById(R.id.tv_home_shop_list_level);
            tvHomeShopListMonthSales = itemView.findViewById(R.id.tv_home_shop_list_month_sales);
            tvHomeShopListUpSend = itemView.findViewById(R.id.tv_home_shop_list_up_send);
            tvHomeShopListBasis = itemView.findViewById(R.id.tv_home_shop_list_basis);
            tvHomeShopListSendDistance = itemView.findViewById(R.id.tv_home_shop_list_send_distance);
            tvHomeShopDistanceHuiLine = itemView.findViewById(R.id.tv_home_shop_distance_hui_line);
            tvHomeShopListSendTime = itemView.findViewById(R.id.tv_home_shop_list_send_time);
            tvDayang = itemView.findViewById(R.id.tv_dayang);
            tvShunfeng = itemView.findViewById(R.id.tv_shunfeng);
            mgvHomeShopList = itemView.findViewById(R.id.mgv_home_shop_list);
            mgvHomeGoodsList = itemView.findViewById(R.id.mgv_home_goods_list);
            mgvHomeShopList.setSelector(new ColorDrawable(Color.TRANSPARENT));
            mgvHomeGoodsList.setSelector(new ColorDrawable(Color.TRANSPARENT));
        }
    }

    public class XfdHolder extends RecyclerView.ViewHolder {

        private ImageView ivXfdGood;
        private TextView tvXfdName;
        private TextView tvXfdTag;
        private TextView tvXfdGoodPrice;
        private TextView tvXfdGoodsSaleNum;

        public XfdHolder(View itemView) {
            super(itemView);
            ivXfdGood = itemView.findViewById(R.id.iv_xfd_good);
            tvXfdName = itemView.findViewById(R.id.tv_xfd_name);
            tvXfdTag = itemView.findViewById(R.id.tv_xfd_tag);
            tvXfdGoodPrice = itemView.findViewById(R.id.tv_xfd_good_price);
            tvXfdGoodsSaleNum = itemView.findViewById(R.id.tv_xfd_goods_sale_num);
        }
    }

    public interface OnItemClick {
        void setOnItemClick(View view, int position);
    }

    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public void onClick(View v) {

        if (onItemClick != null) {
            onItemClick.setOnItemClick(v, (Integer) v.getTag());
        }
    }


    public class Model {
        int imageId;
        String name;
        String level;
        String sales;
        String up_send;
        String basis;
        String send_distance;
        String send_time;
    }


}
